/*
 Copyright (c) Whitemice Consulting, 2007, 2008, 2009
 Adam Tauno Williams <awilliam@whitemice.org>
 License: MIT/X11

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
*/
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using Gtk;
using Whitemice.ZOGI;
using Whitemice.ZOGI.GtkWidgets;
using Whitemice.ZOGI.GtkForms;
using Whitemice.ZOGI.GtkFolios;

namespace Whitemice.ZOGI.GtkExtra
{
  [System.ComponentModel.Category("Extra")]
  [System.ComponentModel.ToolboxItem(true)]
  public partial class ContactSearch : Gtk.Bin, IZOGIWidget
  {
		protected ContactQBEDialog	                  qbeDialog;
		protected Label							                  status;
		protected MultiResultCallback                 searchResultCallback;
		public    EntityEventHandler                  OnContactEdit;
		public    EntityEventHandler                  OnContactRemove;
		
		public ContactSearch()
		{ 
			this.Build();
			
			status = null;
			qbeDialog = null;
			contactList.OnContactEdit = new EntityEventHandler(ContactEdit);
			contactList.OnContactRemove = new EntityEventHandler(ContactRemove);
			//searchResultCallback = new MultiResultCallback(this.ReceiveDataCallback);
			this.ShowAll();
		} // end ctor 
		
		public void Remove(Entity entity)
		{
		  contactList.Remove(entity);
		} // end Remove
		
		public bool PopupEnabled
		{
			get { return contactList.PopupEnabled; }
			set { contactList.PopupEnabled = value; }
		} // end PopupEnabled		

		protected virtual void OnFindClicked(
			object _ender, 
			System.EventArgs args)
		{
      ContactSearchCommand  command;

      if (searchValue.Text.Length > 2)
			{
        command = new ContactSearchCommand("ContactSearch");
        command.AddORCriteria("firstName",
                              string.Format("*{0}*", searchValue.Text),
                              true,
                              false);
        command.AddORCriteria("lastName",
                              string.Format("*{0}*", searchValue.Text),
                              true,
                              false);
        command.Callback = new MultiResultCallback(this.ReceiveDataCallback);
				Query(command);
			} // end found-searchValue-text
		} // end OnFindClicked

		protected virtual void OnQBEClicked(object sender, System.EventArgs args)
		{
			if (qbeDialog == null)
			{
				qbeDialog = new ContactQBEDialog(
					null, 
					Gtk.DialogFlags.DestroyWithParent);
				qbeDialog.OnQBEDialogOk += new System.EventHandler(OnQBEOk);
				qbeDialog.OnQBEDialogCancel += new System.EventHandler(OnQBECancel);
				qbeDialog.Destroyed += new System.EventHandler(OnQBEDestroyed);
			} else
				{
					qbeDialog.Show();
					qbeDialog.Present();
				}
		} // end OnQBEClicked
		
		protected virtual void OnQBEOk(object sender, EventArgs args)
		{
			qbeDialog.Hide();
      var command = qbeDialog.GetQuery(65535, "ContactSearch");
      command.Callback = new MultiResultCallback(this.ReceiveDataCallback);
			Query(command);
		} // end OnQBEOk
		
		protected virtual void OnQBECancel(object sender, EventArgs args)
		{
			qbeDialog.Hide();
		} // end OnQBECancel
		
		protected virtual void OnQBEDestroyed(object sender, EventArgs args)
		{
			qbeDialog = null;
		} // end OnQBEDestroyed
				
		protected virtual void Query(IZOGICommand command)
		{
      Server.Connection.RunCommand(command);
			SetStatus("Contact search dispatched.");
		} // end Query
		
		protected void ContactEdit(
			Entity entity, 
			object context, 
			Exception exception)
		{ OnContactEdit(entity, context, exception); }
		
		protected void ContactRemove(
			Entity entity, 
			object context, 
			Exception exception)
		{ OnContactRemove(entity, context, exception); }
		
		public void ReceiveRPCCall(RPCEventArgs args)
		{
      if (args.HasResults)
      {
			  contactList.Fill(args.Payload, null);
				SetStatus(string.Format("{0} contacts returned from search.", 
                                args.Payload.Count()));
			} else SetStatus("Search result received with no data.");
		} // end SearchResultCallback	
		
		public string Status 
		{
		  set
		  {
		    if (status != null)
		    {
		      status.Markup = value;
		    }
		  }
		} // end Status
		
		public void SetStatus(string message)
		{
		  Status = message;
		} // end SetStatus
		 
		public Gtk.Label StatusLabel
		{
		  set { status = value; }
		}	// end StatusLabel
  } // end class
}
